package dns

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"context"
	"os"
	"testing"

	"github.com/k8gb-io/k8gb/controllers/resolver"
	"github.com/stretchr/testify/assert"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/kubernetes/scheme"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
	externaldnsApi "sigs.k8s.io/external-dns/apis/v1alpha1"
	externaldns "sigs.k8s.io/external-dns/endpoint"
)

func TestCreateZoneDelegation(t *testing.T) {
	// arrange
	getExistingEndpoint := func(name, namespace string) *externaldnsApi.DNSEndpoint {
		return &externaldnsApi.DNSEndpoint{
			ObjectMeta: metav1.ObjectMeta{
				Name:      name,
				Namespace: namespace},
			Spec: externaldnsApi.DNSEndpointSpec{Endpoints: []*externaldns.Endpoint{
				{
					DNSName:    "cloud.example.com",
					RecordTTL:  60,
					Targets:    []string{"gslb-ns-eu-k8gb-test-gslb.cloud.example.com", "gslb-ns-us-k8gb-test-gslb.cloud.example.com"},
					RecordType: "NS",
				},
				{
					DNSName:    "gslb-ns-eu-k8gb-test-gslb.cloud.example.com",
					RecordTTL:  60,
					RecordType: "A",
					Targets:    []string{"10.0.0.1", "10.0.0.2"},
				},
			}},
		}
	}

	getFakeClient := func(ctx context.Context, namespace string, names ...string) client.Client {
		cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
		for _, v := range names {
			_ = cl.Get(ctx, client.ObjectKey{Name: v, Namespace: namespace}, &externaldnsApi.DNSEndpoint{})
			_ = cl.Get(ctx, client.ObjectKey{Name: v, Namespace: namespace}, &externaldnsApi.DNSEndpoint{})
			_ = cl.Update(ctx, &externaldnsApi.DNSEndpoint{
				ObjectMeta: metav1.ObjectMeta{
					Name:      v,
					Namespace: namespace},
			})
		}
		return cl
	}

	getFakeClientForExistingEndpoint := func(ctx context.Context, expectedEP *externaldnsApi.DNSEndpoint) client.Client {
		cl := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(expectedEP).Build()
		_ = cl.Get(ctx, client.ObjectKey{Name: expectedEP.Name, Namespace: expectedEP.Namespace}, expectedEP)
		_ = cl.Update(ctx, &externaldnsApi.DNSEndpoint{
			ObjectMeta: metav1.ObjectMeta{
				Name:      expectedEP.Name,
				Namespace: expectedEP.Namespace},
		})
		return cl
	}

	ctx := context.TODO()
	var tests = []struct {
		name          string
		config        resolver.Config
		expectedError bool
		client        client.Client
	}{
		{
			name: "existing cloud.example.com",
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   60,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						LoadBalancedZone: "cloud.example.com",
						ParentZone:       "example.com",
						NegativeTTL:      60,
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						ClusterNSName:    "gslb-ns-eu-k8gb-test-gslb.cloud.example.com",
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-k8gb-test-gslb.cloud.example.com",
						},
					},
				},
			},
			client:        getFakeClientForExistingEndpoint(ctx, getExistingEndpoint("k8gb-ns-extdns-cloud-example-com", "k8gb")),
			expectedError: false,
		},
		{
			name: "new cloud.example.com",
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   60,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						LoadBalancedZone: "cloud.example.com",
						ParentZone:       "example.com",
						NegativeTTL:      60,
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						ClusterNSName:    "gslb-ns-eu-k8gb-test-gslb.cloud.example.com",
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-k8gb-test-gslb.cloud.example.com",
						},
					},
				},
			},
			client:        getFakeClient(ctx, "k8gb", "k8gb-ns-extdns-cloud-example-com"),
			expectedError: false,
		},
		{
			name: "new cloud.example.com, cloud.example.org",
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   60,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						LoadBalancedZone: "cloud.example.com",
						ParentZone:       "example.com",
						NegativeTTL:      60,
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						ClusterNSName:    "gslb-ns-eu-k8gb-test-gslb.cloud.example.com",
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-k8gb-test-gslb.cloud.example.com",
						},
					},
					{
						LoadBalancedZone: "cloud.example.org",
						ParentZone:       "example.org",
						NegativeTTL:      60,
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						ClusterNSName:    "gslb-ns-eu-k8gb-test-gslb.cloud.example.org",
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-k8gb-test-gslb.cloud.example.org",
						},
					},
				},
			},
			client:        getFakeClient(ctx, "k8gb", "k8gb-ns-extdns-cloud-example-com", "k8gb-ns-extdns-cloud-example-org"),
			expectedError: false,
		},
	}

	// act
	// assert
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			externalDNSProvider := NewExternalDNS(ctx, test.client, test.config)
			for _, zone := range test.config.DelegationZones {
				err := externalDNSProvider.CreateZoneDelegation(zone)
				assert.Equal(t, test.expectedError, err != nil)
			}
		})
	}
}

func TestMain(m *testing.M) {
	scheme.Scheme.AddKnownTypes(schema.GroupVersion{Group: "externaldns.k8s.io", Version: "v1alpha1"}, &externaldnsApi.DNSEndpoint{})
	os.Exit(m.Run())
}
